import { createRouter, createWebHashHistory } from 'vue-router';
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css';

import { appRoutes } from './routes';
import { REDIRECT_MAIN, NOT_FOUND_ROUTE } from './routes/base';
import createRouteGuard from './guard';
import { listenerRouteChange } from '@/utils/route-listener'; // 需要根据实际路径修改
import { setRouteEmitter } from '@/utils/route-listener'; // 根据实际情况修改导入路径

NProgress.configure({ showSpinner: false }); // NProgress Configuration

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: 'login',
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index.vue'),
      meta: {
        requiresAuth: false,
      },
    },
    {
      path: '/fixPassword',
      name: 'fixPassword',
      component: () => import('@/views/login/fiexPassword.vue'),
      meta: {
        requiresAuth: false,
      },
    },
    {
      path: '/qrCodeLogin',
      name: 'qrCodeLogin',
      component: () => import('@/views/login/qrCodeLogin.vue'),
      meta: {
        requiresAuth: false,
      },
    },
    {
      path: '/saoMa',
      name: 'saoMa',
      component: () => import('@/views/login/saoMa.vue'),
      meta: {
        requiresAuth: false,
      },
    },
    ...appRoutes,
    REDIRECT_MAIN,
    NOT_FOUND_ROUTE,
  ],
  scrollBehavior() {
    return { top: 0 };
  },
});
createRouteGuard(router);
router.afterEach((to, from) => {
  setRouteEmitter(to);
  console.log(`已从 ${from.name || '无'} 路由跳转到 ${to.name} 路由`);
  NProgress.done();
});
export default router;
